// index.js

const apiUrl = 'http://localhost:3000';

const tmpAnswer = {
  "id":1,
  "answers":[
    {
      "id":1,
      "question_type":'input',
      "text":"YYYXXX",
    },
    {
      "id":2,
      "question_type":'radio',
      "options":[
        {
          "option_id":"A",
          "option_value":"男"
        }
      ]
    },
    {
      "id":3,
      "question_type":'input',
      "text":"2",
    },
    {
      "id":4,
      "question_type":'checkbox',
      "options":[
        {
          "option_id":"A",
          "option_value":"数学与自然科学"
        },
        {
          "option_id":"C",
          "option_value":"哲学与心理学"
        }
      ]
    }
  ]
};

Page({
  data: {
    qid:1,
    uid:1,
    name:"",
    questions:[],
  },

  getQuestion(qid){
    const that = this;
    wx.request({
      url: apiUrl+'/questionnaire/'+qid,
      success (res) {
        console.log(res.data)
        that.setData({
          name:res.data.name,
          questions:res.data.questions,
        })
      },
      fail(err){
        console.error(err);
      }
    })
  },

  submitAnswer(){
    const that = this;
    wx.request({
      url: apiUrl+'/answer/'+ that.data.uid,
      method:"PUT",
      data:JSON.stringify(tmpAnswer),
      success (res) {
        console.log(res.data)
        
      },
      fail(err){
        console.error(err);
      }
    })
  },
  onLoad() {
    this.getQuestion(this.data.qid);
  },
});
